//
//  HOAddExtraAssistantViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOAddExtraAssistantViewController.h"
#import "HOUtils.h"
#import <AVFoundation/AVFoundation.h>
#import "HOUtils.h"
#import "HORecordView.h"
#import "HORecordActiveView.h"
#import "HODefine.h"
#import "VoiceConverter.h"
#import "HODepartmentViewController.h"
#import "HOFaultCategoryViewController.h"
#import "HOHopeHandlerViewController.h"
#import "HOReportLocationViewController.h"
#import "HOConstants.h"
#import "HOUserInfoEntity.h"
#import "HOUIMgr.h"
#import "HOStaticDataMgr.h"
#import "HOAreaEntity.h"
#import "HOAssignViewController.h"
#import "HOHopeHanderEntity.h"
#import "HOEditInfoViewController.h"
#import "HODefine.h"
#import "HOReplyViewController.h"
#import "HOConfirmOrderViewController.h"
#import "JSHAREService.h"
#import "HOExtraAssCheckViewController.h"
#import "HOReportDetailEntity.h"
#import "HOExtraAssistantConfirmViewController.h"
#import "HOReportDetailViewController.h"

@interface HOAddExtraAssistantViewController()<HOReportLocationViewControllerDelegate, HOAssignViewControllerDelegate, HOEditInfoViewControllerDelegate>
{
	
}
@property(nonatomic, retain)HOUserInfoEntity *userInfo;
@property(nonatomic, retain)NSString *faultDesc;  //影响范围
@property(nonatomic, retain)NSArray *fixTimes;  //
@property(nonatomic, retain)NSString *effectScope;  //影响范围
@property(nonatomic, retain)NSString *budget;  //维修预算
@property(nonatomic, retain)NSString *extraMobile;  //外协电话
@property(nonatomic, retain)NSString *extraName;  //外协名称
@property(nonatomic, assign)NSInteger fixTime;  //0:24小时, 1:48小时  2:5个日历天
@property(nonatomic, retain)UIView	*menuPanel;
@property(nonatomic, assign)BOOL isFromOriginReport;
@property(nonatomic, assign)BOOL isInitDefaultImage;

@end

@implementation HOAddExtraAssistantViewController

- (void)viewDidLoad
{
	[super viewDidLoad];
//	self.faultDesc = @"kdslkf jslfkj skljf lasjklfdj laksfjlk asjklf dsakl fdjkljlkjfkla jlfkd jslkj lkj a afd";
//	self.effectScope = @"kdslkf jslfkj skljf lasjklfdj laksfjlk asjklf dsakl fdjkljlkjfkla jlfkd jslkj lkj a afd";
	self.menuPanel.hidden = YES;
	[self initTableView];
	[self initSubtViews];
	[self initMediaView];
	[self registerTapGesture];
	[self initDataSource];
	[self requestUserInfo];
	[self registerKeyboard];
	[self initPickerView];
//	[self initTextField];
}


#pragma mark
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self showPickerView:NO];
	[self hideKeyboard];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 3;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return [self.fixTimes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	pickerViewSelectRow = row;
}

- (void)didPickDone:(id)sender
{
	[super didPickDone:nil];
	self.fixTime = pickerViewSelectRow;
	[baseTableview reloadData];
}

#pragma mark -- picker end


- (void)initDataSource
{
	self.userInfo = [[HOStaticDataMgr sharedInstance] getUserInfo];
	self.fixTimes = [NSArray arrayWithObjects:@"24小时", @"48小时", @"5个日历天", nil];
}

- (void)requestUserInfo
{
	if(!self.userInfo.isGotData)
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetUserInfo}];
}

- (void)initSubtViews
{
	if(self.isDetailDisplayModal == NO)
		baseTableview.tableFooterView = [self getFooterViewWithTitle:@"提交"];
}

- (void)didClickFooterButton:(UIButton *)btn
{
	if(self.faultDesc == nil || self.faultDesc.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写故障情况描述！", @"请填写故障情况描述！")];
		return;
	}
	
	if(self.effectScope == nil || self.effectScope.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写影响范围！", @"请填写影响范围！")];
		return;
	}
	
	if(self.budget == nil || self.budget.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写维修预算！", @"请填写维修预算！")];
		return;
	}
	
	NSString *areaIds = [self getSelectArea];
	if(areaIds.length == 0)
	{
		[self popupHintView:@"请选择故障地点!"];
		return;
	}
	int deptId = self.userInfo.departmentId;
	if(self.orderDetailEntity)
	{
		deptId = self.orderDetailEntity.postDepartmentId;
	}
	if(deptId == 0)
	{
		[self popupHintView:@"申请人科室异常，无法申请外协单!"];
		return;
	}
	
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[super getMediaDic]];
	if(dic == nil)
		return;
	[dic addEntriesFromDictionary:@{kHO_FIELD_Action:kHO_ACTION_AddExternalFault,
									kHO_FIELD_Incidence:[HOUtils getValideString:self.effectScope],
									kHO_FIELD_FaultDetail:[HOUtils getValideString:self.faultDesc],
									kHO_FIELD_ProposalTime:[NSNumber numberWithInteger:pickerViewSelectRow],
									kHO_FIELD_Budget:[HOUtils getValideString:self.budget],
									kHO_FIELD_AreaId:[HOUtils getValideString:[self getSelectArea]],
									kHO_FIELD_DirectorId:[NSNumber numberWithInt:[[HOStaticDataMgr sharedInstance] getDirectorId]],
									kHO_FIELD_ExternalMobile:[HOUtils getValideString:self.extraMobile],
									kHO_FIELD_ExternalCompany:[HOUtils getValideString:self.extraName],
									kHO_FIELD_DepartmentId:[NSNumber numberWithInt:self.userInfo.departmentId],
									}];
	if(self.orderDetailEntity)
	{
		[dic setObject:[NSNumber numberWithInt:self.orderDetailEntity.orderSN] forKey:kHO_FIELD_OrderId];
	}
	[self sendRequestWithParams:dic];

}

- (NSDictionary *)getReportDic
{
	NSString *faultDetail = [self getTrimingCharaFromTextView:[self.reportDescEditView getTextView]];
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[super getMediaDic]];
	[dic setObject:kHO_ACTION_AddNormalReport forKey:kHO_FIELD_Action];
	[dic setObject:faultDetail forKey:kHO_FIELD_FaultDetail];
	
	
	NSString *areaIds = [self getSelectArea];	[dic setObject:areaIds forKey:kHO_FIELD_AreaId];
	[dic setObject:[NSNumber numberWithInt:self.selectedDepartmentEntity.departmentId] forKey:kHO_FIELD_DepartmentId];
	if(self.hopeHandler)
	{
		[dic setObject:[NSNumber numberWithInt:self.hopeHandler.userId] forKey:kHO_FIELD_HopeId];
	}
	[dic setObject:[self getSelectTypeIds] forKey:kHO_FIELD_FaultId];
	
	return dic;
}

- (void)submit
{
	[super submit];
	NSDictionary *dic = [self getReportDic];
	if(dic == nil)
		return;
	[self sendRequestWithParams:dic];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
	[super onKeyboardWillShow:notification];
	[baseTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[self hideKeyboard];
}

#pragma mark --

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self hideKeyboard];
	[self resetTableViewFrame];
	if(self.isDetailDisplayModal)
	{
		[self setNavigationItemTitle:@"外协单详情"];
		[self displayRightButtonItemWithTitle:NSLocalizedString(@"更多", @"更多")];
	}
	else
	{
		[self setNavigationItemTitle:@"申请外协单"];
		[self displayRightButtonItemWithTitle:NSLocalizedString(@"重置", @"重置")];
	}
	if(self.orderDetailEntity)
	{
		self.faultDesc = self.orderDetailEntity.detail;
	}
	[baseTableview reloadData];
	if(self.isDetailDisplayModal)
	{
		[self requestDetailData];
	}
	self.menuPanel = [self createMenuItem];
	baseTableview.frame = CGRectMake(0, 0, self.view.frame.size.width, [HOUtils getScreenSize].height - [HOUtils getStatusBarHeight] - 44);
	[self initBaseData];
	[self loadDefaultImage];
}

- (void)loadDefaultImage
{
	if(self.isInitDefaultImage)
		return;
	self.isInitDefaultImage = YES;
	if(self.isDetailDisplayModal)
	{
		self.uploadImgView.needDisplayRemoveButton = NO;
		[self.uploadImgView setImagePaths:self.extraAssistantEntity.imageList];
		[self.uploadImgView hideUploadButton];
		
	}
	else
	{
		if(self.orderDetailEntity)
		{
			[self.uploadImgView setImagePaths:self.orderDetailEntity.imageList];
		}
	}
}

- (void)requestOriginReport
{
	if(self.extraAssistantEntity.orderId == 0)
		return;
	//10000314
	int orderId = self.extraAssistantEntity.orderId - 10000000;
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetOrderDetail,
								  kHO_FIELD_OrderId:[NSNumber numberWithInt:orderId]}];
}

- (void)initBaseData
{
	if(self.orderDetailEntity == nil)
	{
		
//		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetOrderDetail,
//									  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.extraAssistantEntity.orderSN]}];
		return;
	}
	self.selectedFirstAreaEntity = self.orderDetailEntity.firstAreaEntity;
	self.selectedSecondAreaEntity = self.orderDetailEntity.secondAreaEntity;
	self.selectedThirdAreaEntity = self.orderDetailEntity.thirdAreaEntity;
	[baseTableview reloadData];
}

- (void)requestDetailData
{
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{kHO_FIELD_Action:kHO_ACTION_GetExternalFaultDetail,
																			   kHO_FIELD_OrderId:[NSNumber numberWithInt:self.extraAssistantEntity.orderId]}];
	if(self.companyId)
	{
		[dic setObject:self.companyId forKey:kHO_FIELD_CompanyID];
	}
	
	if(self.orderDetailEntity != nil)
	{
		[dic setObject:self.companyId forKey:kHO_FIELD_CompanyID];
	}
	
	[self sendRequestWithParams:dic];
}

- (UIView *)createMenuItem
{
	CGFloat num = 0;
	UIView *view = [[[UIView alloc]
					 initWithFrame:CGRectMake(self.view.frame.size.width - 130,
											  0,
											  130,
											  num * 35)] autorelease];
	[view setBackgroundColor:[UIColor whiteColor]];
	int myUserId =  [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId];
	
	if(self.extraAssistantEntity.postId == myUserId)
	{
		//当前申请外协单人
		/*
		 添加回复
		 确认完成
		 qq分享
		 微信分享
		 */
		
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:num * 35]];
		num ++;
		
		if([[HOStaticDataMgr sharedInstance] isDirectorUser] && self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_UNCHECK)
		{
			//需要显示 审核
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"审核外协单", @"审核外协单") withSelector:@selector(checkReport:) withY:num * 35]];
			num ++;
		}
		if((self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_DIR_CHECK && self.extraAssistantEntity.deanId == 0)
		   || self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_DEAN_CHECK)
		{
			//主任申请的，目前主任审核而又不需要院长审核，需要分享
			//需要分享
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"QQ分享", @"QQ分享") withSelector:@selector(shareReportByQQ:) withY:num * 35]];
			num ++;
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"微信分享", @"微信分享") withSelector:@selector(shareReportByWechat:) withY:num * 35]];
			num ++;
		}
		if(self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_REPAIRED)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"确认完成", @"确认完成") withSelector:@selector(confirmComplete:) withY:num * 35]];
			num ++;
		}
	}
	else if(self.extraAssistantEntity.directorId == myUserId)
	{
		//主任用户
		/*
		 添加回复
		 审核协助单
		 */
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:num * 35]];
		num ++;
//		NSLog(@"[HOStaticDataMgr sharedInstance] getCompanyId]:%@", [[HOStaticDataMgr sharedInstance] getCompanyId]);
//		NSLog(@"self.extraAssistantEntity.companyId:%@", self.extraAssistantEntity.companyId);
		BOOL isBeloneToSameCourtyard = [[[HOStaticDataMgr sharedInstance] getCompanyId] isEqualToString: self.extraAssistantEntity.companyId];
		if(self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_UNCHECK && isBeloneToSameCourtyard)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"审核外协单", @"审核外协单") withSelector:@selector(checkReport:) withY:num * 35]];
			num ++;
		}
	}
	else if(self.extraAssistantEntity.deanId == myUserId)
	{
		//副院长用户
		/*
		 添加回复
		 审核协助单
		 */
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:num * 35]];
		num ++;
		if(self.extraAssistantEntity.status == HO_EXTRA_ASS_ORDER_DIR_CHECK)
		{
			[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"审核外协单", @"审核外协单") withSelector:@selector(checkReport:) withY:num * 35]];
			num ++;
		}
	}
	else
	{
		
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"添加回复记录", @"添加回复记录") withSelector:@selector(addComment:) withY:num * 35]];
		num ++;
	}
	if(self.isFromOriginReport)
	{
		[view addSubview:[self createMenuItemWithTitle:NSLocalizedString(@"查看原报修单", @"查看原报修单") withSelector:@selector(viewOriginReport:) withY:num * 35]];
		num ++;
	}
	
	[view setBackgroundColor:[UIColor whiteColor]];
	view.layer.shadowOffset = CGSizeMake(0.0, 0.0); //shadow on the right and bottom sides.
	view.layer.shadowRadius = 4.0;
	view.layer.shadowOpacity = 0.25;
	[self.view addSubview:view];
	view.hidden = YES;
	view.frame = CGRectMake(self.view.frame.size.width - 130,
							0,
							130,
							num * 35);
	return view;
}

- (void)viewOriginReport:(id)inder
{
	HOReportDetailViewController *viewCtrl = [[HOReportDetailViewController alloc] init];
	HOReportOrderEntity *entity = [[[HOReportOrderEntity alloc] init] autorelease];
	viewCtrl.reportType = HO_REPORT_TYPE_NORMAL;
	entity.orderId = self.extraAssistantEntity.orderId - 10000000;
	viewCtrl.orderEntity = entity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (UIButton *)createMenuItemWithTitle:(NSString *)title withSelector:(SEL)selector withY:(CGFloat)y
{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
	btn.frame = CGRectMake(- 1,
						   y,
						   132,
						   35);
	
	[btn setTitle:title forState:UIControlStateNormal];
	btn.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
	btn.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	[btn setTitleColor:TEXT_COLOR_666666 forState:UIControlStateNormal];
	btn.titleLabel.font = [UIFont systemFontOfSize:14];
	[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
	return btn;
}


- (void)shareReportByQQ:(UIButton *)sender
{
	[self shareReport:JSHAREPlatformQQ];
}
- (void)shareReportByWechat:(UIButton *)sender
{
	[self shareReport:JSHAREPlatformWechatSession];
}


- (void)shareReport:(JSHAREPlatform)platform
{
	JSHAREMessage *message = [JSHAREMessage message];
	message.mediaType = JSHARELink;
	message.url = [NSString stringWithFormat:@"http://www.baoxiuba.cn/external.html?OrderId=%d&CompanyId=%@", self.extraAssistantEntity.orderSN, self.userInfo.companyId];
	message.text = self.extraAssistantEntity.faultDetail;
	message.title = @"您有来自邵医报修外协申请单";
	message.platform = platform;
	NSString *imageURL = @"http://www.baoxiuba.cn/Public/img/syfIcon.png";
	NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
	
	message.image = imageData;
	[JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
		
//		LogImp(@"分享回调");
		
	}];
}
- (void)checkReport:(UIButton *)sender
{
	//审核
	self.menuPanel.hidden = YES;
	HOExtraAssCheckViewController *viewCtrl = [[HOExtraAssCheckViewController alloc] init];
	viewCtrl.extraAssistantEntity = self.extraAssistantEntity;
	int myUserId = [[[HOStaticDataMgr sharedInstance] getUserInfo] getUserId];
	viewCtrl.isDirUser = (self.extraAssistantEntity.directorId == myUserId);
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)addComment:(UIButton *)sender
{
	self.menuPanel.hidden = YES;
	HOReplyViewController *viewCtrl = [[HOReplyViewController alloc] init];
	viewCtrl.entity = (HOReportDetailEntity *)self.extraAssistantEntity;
	viewCtrl.isCheckModal = YES;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)confirmComplete:(UIButton *)sender
{
	HOExtraAssistantConfirmViewController *viewCtrl = [[HOExtraAssistantConfirmViewController alloc] init];
	viewCtrl.extraAssistantEntity = self.extraAssistantEntity;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)removeReport:(UIButton *)sender
{
	HOAlertView *amAlertView = [[HOAlertView alloc] initWithTitle:NSLocalizedString(@"您确认删除此报修吗?",  @"您确认删除此报修吗?")
														  message: nil
														 delegate: self
												cancelButtonTitle:NSLocalizedString(@"取消", @"取消")
												otherButtonTitles:NSLocalizedString(@"确定", @"确定"), nil];
	amAlertView.tag = [kHO_DIALOG_RemoveReport integerValue];
	[amAlertView show];
	[amAlertView release];
}

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	if([action isEqualToString:kHO_CMD_Report_Detail_Changed])
	{
		[self requestDetailData];
	}
}

- (void)rightButtonItemDidClick
{
	if(self.isDetailDisplayModal)
	{
		self.menuPanel.hidden = !self.menuPanel.hidden;
	}
	else
	{
		[self cleanup];
	}
}

- (void)dealloc
{
	self.userInfo = nil;
	self.orderDetailEntity = nil;
	[super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
		return 44;
	if(indexPath.section == 4)
	{
		if(self.isDetailDisplayModal)
		{
			if(self.extraAssistantEntity == nil || self.extraAssistantEntity.imageList == nil || ![self.extraAssistantEntity.imageList isKindOfClass:[NSArray class]] || [self.extraAssistantEntity.imageList count] == 0)
			{
				return 0;
			}
		}
		CGFloat height = [self getPhotoViewHeight];
		return height;
	}
	else if(indexPath.section == 3)
	{
		if(indexPath.row == 3)
		{
			NSString *content = @"";
			CGSize contentSize = [HOUtils findHeightForText:content havingWidth:(tableView.frame.size.width - (self.isDetailDisplayModal ? 110 : 130)) andFont:[UIFont systemFontOfSize:13]];
			CGFloat height = MAX(22, contentSize.height);
			return height + 22;
		}
		else
		{
			return 44;
		}
	}
	else
	{
		NSString *content = @"";
		
		if(self.isDetailDisplayModal)
		{
			if(indexPath.section == 1)
			{
				content = [HOUtils getValideString:self.extraAssistantEntity.faultDetail];
			}
			else if(indexPath.section == 2)
			{
				content = [HOUtils getValideString:self.extraAssistantEntity.effectScope];
			}
		}
		else
		{
			if(indexPath.section == 1)
			{
				content = [HOUtils getValideString:self.faultDesc];
			}
			else if(indexPath.section == 2)
			{
				content = [HOUtils getValideString:self.effectScope];
			}
		}
		
		
		CGSize contentSize = [HOUtils findHeightForText:content
											havingWidth:(tableView.frame.size.width - (self.isDetailDisplayModal ? 110 : 130))
												andFont:[UIFont systemFontOfSize:13]];
		CGFloat height = MAX(22, contentSize.height);
		return height + 22;
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 5;
	else if(section == 1)
		return 1;
	else if(section == 2)
		return 1;
	else if(section == 3)
		return 4;
	else if(section == 4)
		return 1;
	else
		return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
	{
		if(self.isDetailDisplayModal)
		{
			return 60;
		}
		return 0.0001;
	}
	else if(section == 1
			|| section == 2
			|| section == 3
			|| section == 4)
		return SECTION_PADDING / 2;
	return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)] autorelease];
	if(self.isDetailDisplayModal && section == 0)
	{
		UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(15, 5, tableView.frame.size.width, 2)] autorelease];

		lbl.text = [NSString stringWithFormat:@"单号:%d  状态:", self.extraAssistantEntity.orderId];
		lbl.font = [UIFont systemFontOfSize:14];
		[lbl sizeToFit];
		
		UILabel *orderStatusLbl = [[[UILabel alloc] initWithFrame:CGRectMake(lbl.frame.size.width + 15 + 2, 5, 130, 22)] autorelease];
		orderStatusLbl.font = [UIFont systemFontOfSize:14];
		orderStatusLbl.text =  [self.extraAssistantEntity getStatusString];
		orderStatusLbl.textColor = [HOUtils getOrderColorWithStatus:self.extraAssistantEntity.status];
		[orderStatusLbl sizeToFit];
//		orderStatusLbl.layer.borderWidth = 1;
		[view addSubview:lbl];
		[view addSubview:orderStatusLbl];
		
		UILabel *timeLbl = [[[UILabel alloc] initWithFrame:CGRectMake(15, 35, tableView.frame.size.width, 2)] autorelease];
		
		timeLbl.text = [NSString stringWithFormat:@"申请时间:"];
		timeLbl.font = [UIFont systemFontOfSize:14];
		[timeLbl sizeToFit];
		
		UILabel *timeValueLbl = [[[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 210, 35, 200, 22)] autorelease];
		timeValueLbl.font = [UIFont systemFontOfSize:14];
		timeValueLbl.textAlignment = NSTextAlignmentRight;
		timeValueLbl.text = self.extraAssistantEntity.theNumberDay;
//		timeValueLbl.layer.borderWidth = 1;
		[view addSubview:timeLbl];
		[view addSubview:timeValueLbl];
	}
	
	return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		static NSString *HOReportViewCellIdentifier = @"HOReportViewCellIdentifier";
		HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOReportViewCellIdentifier];
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOReportViewCellIdentifier] autorelease];
		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.textLabel.textColor = TEXT_COLOR_181818;
		cell.textLabel.font = [UIFont systemFontOfSize:15];
		UILabel *detailLbl = (UILabel *)[cell viewWithTag:114];
		if(detailLbl == nil)
		{
			detailLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(100,
												   (cellHeight - 22) / 2,
												   tableView.frame.size.width - (self.isDetailDisplayModal ? 110 : 130),
												   22)] autorelease];
			detailLbl.tag = 114;
			detailLbl.font = [UIFont systemFontOfSize:15];
			detailLbl.textColor = TEXT_COLOR_666666;
			detailLbl.textAlignment = NSTextAlignmentRight;
			[cell.contentView addSubview:detailLbl];
		}
		detailLbl.hidden = YES;
		detailLbl.numberOfLines = 1;
		if(indexPath.section == 0)
		{
			detailLbl.hidden = NO;
			if(indexPath.row == 0)
			{
				cell.textLabel.text = NSLocalizedString(@"申请人：", @"申请人：");
				if(self.orderDetailEntity)
				{
					detailLbl.text = self.orderDetailEntity.reporterName;
				}
				else
				{
//					detailLbl.text = self.userInfo.realName;
				}
			}
			else if(indexPath.row == 1)
			{
				cell.textLabel.text = NSLocalizedString(@"申请科室:", @"申请科室：");
				if(self.isDetailDisplayModal)
				{
					detailLbl.text = self.extraAssistantEntity.departmentName;
				}
				else
				{
					if(self.orderDetailEntity != nil)
					{
						detailLbl.text = self.orderDetailEntity.postDepartmentName;
					}
					else
					{
						detailLbl.text = self.userInfo.departmentName;
					}
				}
			}
			else if(indexPath.row == 2)
			{
				cell.textLabel.text = NSLocalizedString(@"申请人电话:", @"申请人电话：");
				if(self.isDetailDisplayModal)
				{
					detailLbl.text = self.extraAssistantEntity.telephone;
				}
				else
				{
					if(self.orderDetailEntity != nil)
					{
						detailLbl.text = self.orderDetailEntity.phoneNumber;
					}
					else
					{
						detailLbl.text = self.userInfo.mobile;
					}
				}
			}
			else if(indexPath.row == 3)
			{
				cell.textLabel.text = NSLocalizedString(@"故障位置：", @"故障位置：");
				detailLbl.numberOfLines = 2;
				NSString *content = @"";
				if(self.isDetailDisplayModal)
				{
					cell.accessoryType = UITableViewCellAccessoryNone;
					cell.selectionStyle = UITableViewCellSelectionStyleNone;
					content = [HOUtils getValideString:self.extraAssistantEntity.faultAddress];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"无", @"无");
					}
				}
				else
				{
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					cell.selectionStyle = UITableViewCellSelectionStyleGray;
					content = [HOUtils getValideString:[self getSelectAreaName]];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"点击进入选择", @"点击进入选择");
					}
				}
				
				detailLbl.text = content;
				[detailLbl sizeToFit];
				detailLbl.frame = CGRectMake(100,
											 (cellHeight - detailLbl.frame.size.height) / 2,
											 tableView.frame.size.width - (self.isDetailDisplayModal ? 110 : 130),
											 detailLbl.frame.size.height);
			}
			else if(indexPath.row == 4)
			{
				cell.textLabel.text = NSLocalizedString(@"经办人:", @"经办人:");
				if(self.isDetailDisplayModal)
				{
					detailLbl.text = self.extraAssistantEntity.trueName;
				}
				else
				{
					detailLbl.text = self.userInfo.realName;
				}
			}
		}
		return cell;
	}
	else if(indexPath.section == 4)
	{
		static NSString *HOTakePhotoTableViewCellIdentifier = @"HOTakePhotoTableViewCellIdentifier";
		HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOTakePhotoTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HOTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOTakePhotoTableViewCellIdentifier] autorelease];
		}
		//	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		cell.textLabel.font = [UIFont systemFontOfSize:15];
		cell.textLabel.textColor = TEXT_COLOR_181818;
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
#pragma mark -- upload image
		HOUploadImageView *uploadImgView = (HOUploadImageView *)[cell viewWithTag:114];
		if(uploadImgView == nil)
		{
			[cell.contentView addSubview:self.uploadImgView];
			uploadImgView.tag = 114;
			
			self.uploadImgView.frame = CGRectMake(0,
												  5,
												  self.view.frame.size.width,
												  self.singleImgWidth);
		}

		return cell;
	}
	else
	{
		static NSString *HOExtraAssistantCellIdentifier = @"HOExtraAssistantCellIdentifier";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOExtraAssistantCellIdentifier];
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: HOExtraAssistantCellIdentifier] autorelease];
		
//		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.textLabel.textColor = TEXT_COLOR_181818;
		cell.textLabel.font = [UIFont systemFontOfSize:15];
		UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(100,
																 4,
																 (tableView.frame.size.width - (self.isDetailDisplayModal ? 110 : 130)),
																 50)];
		lbl.font = [UIFont systemFontOfSize:14];
		lbl.textColor = TEXT_COLOR_999999;
		lbl.textAlignment = NSTextAlignmentRight;
//		lbl.layer.borderWidth = 1;
		[cell.contentView addSubview:lbl];
		NSString *content = @"";
		if(indexPath.section == 1)
		{
			lbl.numberOfLines = 0;
			cell.textLabel.text = NSLocalizedString(@"故障描述:", @"故障描述:");
			if(self.isDetailDisplayModal)
			{
				cell.accessoryType = UITableViewCellAccessoryNone;
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
				content = [HOUtils getValideString:self.extraAssistantEntity.faultDetail];
				if(content.length == 0)
				{
					content = NSLocalizedString(@"无", @"无");
				}
			}
			else
			{
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.selectionStyle = UITableViewCellSelectionStyleGray;
				content = [HOUtils getValideString:self.faultDesc];
				if(content.length == 0)
				{
					content = NSLocalizedString(@"点击进入编辑", @"点击进入编辑");
				}
			}
		}
		else if(indexPath.section == 2)
		{
			lbl.numberOfLines = 0;
			cell.textLabel.text = NSLocalizedString(@"影响范围:", @"影响范围:");
			
			if(self.isDetailDisplayModal)
			{
				cell.accessoryType = UITableViewCellAccessoryNone;
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
				content = [HOUtils getValideString:self.extraAssistantEntity.effectScope];
				if(content.length == 0)
				{
					content = NSLocalizedString(@"无", @"无");
				}
			}
			else
			{
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.selectionStyle = UITableViewCellSelectionStyleGray;
				content = [HOUtils getValideString:self.effectScope];
				if(content.length == 0)
				{
					content = NSLocalizedString(@"点击进入编辑", @"点击进入编辑");
				}
			}
		}
		else if(indexPath.section == 3)
		{
			lbl.numberOfLines = 1;
			if(indexPath.row == 0)
			{
				cell.textLabel.text = NSLocalizedString(@"建议修复时间:", @"建议修复时间:");
				lbl.frame = CGRectMake(120, 11, (tableView.frame.size.width - (self.isDetailDisplayModal ? 130 : 150)), 22);
				
				if(self.isDetailDisplayModal)
				{
					cell.accessoryType = UITableViewCellAccessoryNone;
					cell.selectionStyle = UITableViewCellSelectionStyleNone;
					content = [self.fixTimes objectAtIndex:self.extraAssistantEntity.fixTime];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"无", @"无");
					}
				}
				else
				{
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					cell.selectionStyle = UITableViewCellSelectionStyleGray;
					content = [self.fixTimes objectAtIndex:self.fixTime];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"点击进入选择", @"点击进入选择");
					}
				}
			}
			else if(indexPath.row == 1)
			{
				cell.textLabel.text = NSLocalizedString(@"维修预算:", @"维修预算:");
				lbl.frame = CGRectMake(100, 11, (tableView.frame.size.width - (self.isDetailDisplayModal ? 120 : 140)), 22);
				
				if(self.isDetailDisplayModal)
				{
					cell.accessoryType = UITableViewCellAccessoryNone;
					cell.selectionStyle = UITableViewCellSelectionStyleNone;
					content = [HOUtils getValideString:self.extraAssistantEntity.budget];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"无", @"无");
					}
				}
				else
				{
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					cell.selectionStyle = UITableViewCellSelectionStyleGray;
					content = [HOUtils getValideString:self.budget];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"点击进入编辑", @"点击进入编辑");
					}
				}
			}
			
			else if(indexPath.row == 2)
			{
				cell.textLabel.text = NSLocalizedString(@"外协电话:", @"外协电话:");
				lbl.frame = CGRectMake(100, 11, (tableView.frame.size.width - (self.isDetailDisplayModal ? 120 : 140)), 22);
				if(self.isDetailDisplayModal)
				{
					cell.accessoryType = UITableViewCellAccessoryNone;
					cell.selectionStyle = UITableViewCellSelectionStyleNone;
					content = [HOUtils getValideString:self.extraAssistantEntity.extraMobile];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"无", @"无");
					}
				}
				else
				{
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					cell.selectionStyle = UITableViewCellSelectionStyleGray;
					content = [HOUtils getValideString:self.extraMobile];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"点击进入编辑", @"点击进入编辑");
					}
				}
			}
			else if(indexPath.row == 3)
			{
				lbl.numberOfLines = 0;
				cell.textLabel.text = NSLocalizedString(@"外协名称:", @"外协名称:");
				lbl.frame = CGRectMake(100, 11, (tableView.frame.size.width - (self.isDetailDisplayModal ? 120 : 140)),22);
				if(self.isDetailDisplayModal)
				{
					cell.accessoryType = UITableViewCellAccessoryNone;
					cell.selectionStyle = UITableViewCellSelectionStyleNone;
					content = [HOUtils getValideString:self.extraAssistantEntity.extraName];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"无", @"无");
					}
				}
				else
				{
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					cell.selectionStyle = UITableViewCellSelectionStyleGray;
					content = [HOUtils getValideString:self.extraName];
					if(content.length == 0)
					{
						content = NSLocalizedString(@"点击进入编辑", @"点击进入编辑");
					}
				}
			}
		}
		lbl.text = content;
		[lbl sizeToFit];
		lbl.frame = CGRectMake(tableView.frame.size.width - lbl.frame.size.width - (self.isDetailDisplayModal ? 10 : 30), 12.5, lbl.frame.size.width, lbl.frame.size.height);
		return cell;
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(self.isDetailDisplayModal)
		return;
	if(indexPath.section == 0)
	{
		if(indexPath.row == 3)
		{
			HOReportLocationViewController *viewCtrl = [[HOReportLocationViewController alloc] init];
			viewCtrl.delegate = self;
			viewCtrl.selectedFirstAreaEntity = self.selectedFirstAreaEntity;
			viewCtrl.selectedSecondAreaEntity = self.selectedSecondAreaEntity;
			viewCtrl.selectedThirdAreaEntity = self.selectedThirdAreaEntity;
//			[viewCtrl initSearchBar];
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
		}
	}
	
	else if(indexPath.section == 1)
	{
		HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
		viewCtl.limitedCount = 150;
		viewCtl.delegate = self;
		viewCtl.navItemTitle = NSLocalizedString(@"请输入故障描述", @"请输入故障描述");
		viewCtl.placeHolder = NSLocalizedString(@"请输入故障描述", @"请输入故障描述");
		viewCtl.needHideLimitedLbl = NO;
		viewCtl.isOneLine = NO;
        viewCtl.fieldName = kHO_FIELD_DestDesc;
		viewCtl.oldValue = self.faultDesc;
		viewCtl.tag = HO_TAG_FAULT_DESC;
		[self.navigationController pushViewController:viewCtl animated:YES];
	}
	else if(indexPath.section == 2)
	{
		HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
		viewCtl.limitedCount = 150;
		viewCtl.delegate = self;
		viewCtl.navItemTitle = NSLocalizedString(@"请输入影响范围", @"请输入影响范围");
		viewCtl.placeHolder = NSLocalizedString(@"请输入影响范围", @"请输入影响范围");
		viewCtl.needHideLimitedLbl = NO;
		viewCtl.isOneLine = NO;
		viewCtl.oldValue = self.effectScope;
        viewCtl.fieldName = kHO_FIELD_EffectScope;
		viewCtl.tag = HO_TAG_EFFECT_SCOPE;
		[self.navigationController pushViewController:viewCtl animated:YES];
	}
	else if(indexPath.section == 3)
	{
		if(indexPath.row == 0)
		{
			[self showPickerView:YES];
		}
		else if(indexPath.row == 1)
		{
			HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
			viewCtl.limitedCount = 15;
			viewCtl.delegate = self;
			viewCtl.navItemTitle = NSLocalizedString(@"请输入维修预算", @"请输入维修预算");
			viewCtl.keypadType = UIKeyboardTypeNumberPad;
			viewCtl.placeHolder = NSLocalizedString(@"请输入维修预算", @"请输入维修预算");
			viewCtl.needHideLimitedLbl = YES;
			viewCtl.oldValue = self.budget;
            viewCtl.fieldName = kHO_FIELD_Budget;
			viewCtl.tag = HO_TAG_BUDGE;
			[self.navigationController pushViewController:viewCtl animated:YES];
		}
		
		else if(indexPath.row == 2)
		{
			HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
			viewCtl.limitedCount = 15;
			viewCtl.delegate = self;
			viewCtl.navItemTitle = NSLocalizedString(@"请输入外协电话", @"请输入外协电话");
			viewCtl.keypadType = UIKeyboardTypeNumberPad;
			viewCtl.placeHolder = NSLocalizedString(@"请输入外协电话", @"请输入外协电话");
			viewCtl.needHideLimitedLbl = YES;
			viewCtl.oldValue = self.extraMobile;
            viewCtl.fieldName = kHO_FIELD_Mobile;
			viewCtl.tag = HO_TAG_EXTRA_MOBILE;
			[self.navigationController pushViewController:viewCtl animated:YES];
		}
		
		else if(indexPath.row == 3)
		{
			HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
			viewCtl.limitedCount = 100;
			viewCtl.delegate = self;
			viewCtl.navItemTitle = NSLocalizedString(@"请输入外协名称", @"请输入外协名称");
//			viewCtl.keypadType = UIKeyboardTypeNumberPad;
			viewCtl.placeHolder = NSLocalizedString(@"请输入外协名称", @"请输入外协名称");
			viewCtl.needHideLimitedLbl = YES;
            viewCtl.fieldName = kHO_FIELD_ExternalCompany;
			viewCtl.oldValue = self.extraName;
			viewCtl.tag = HO_TAG_EXTRA_NAME;
			[self.navigationController pushViewController:viewCtl animated:YES];
		}
	}
}
- (void)didFillInContent:(NSString *)content withTag:(NSInteger)tag
{
	if(tag == HO_TAG_FAULT_DESC)
	{
		self.faultDesc = content;
	}
	else if(tag == HO_TAG_EFFECT_SCOPE)
	{
		self.effectScope = content;
	}
	else if(tag == HO_TAG_BUDGE)
	{
		self.budget = content;
	}
	else if(tag == HO_TAG_EXTRA_MOBILE)
	{
		self.extraMobile = content;
	}
	else if(tag == HO_TAG_EXTRA_NAME)
	{
		self.extraName = content;
	}
	[baseTableview reloadData];
}


- (void)didSelectRepairer:(HORepairerEntity *)entity;
{
	self.hopeHandler = entity;
	[baseTableview reloadData];
}


- (void)cleanup
{
	self.hopeHandler = nil;
	self.faultDesc = nil;
	self.effectScope = nil;
	self.budget = nil;
	self.extraMobile = nil;
	self.extraName = nil;
	pickerViewSelectRow = 0;
	[super cleanup];
	[self hideKeyboard];
	[baseTableview reloadData];
}
#pragma mark

- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetOrderDetail] && [response isRequestFailed])
	{
		[self showLoadingView:NO];
	}
	else
	{
		[super didReceiveResponse:response];
	}
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetUserInfo])
	{
		NSDictionary *dic = response.dataNode;
		[self.userInfo parseDic:dic];
		[baseTableview reloadData];
	}
	else if([response isEqualAction:kHO_ACTION_GetOrderDetail])
	{
		self.isFromOriginReport = YES;
		self.menuPanel = [self createMenuItem];
	}
	else if([response isEqualAction:kHO_ACTION_GetExternalFaultDetail])
	{
		NSDictionary *dic = response.dataNode;
		[self.extraAssistantEntity parseDic:dic];
		[baseTableview reloadData];
		if(self.menuPanel)
		{
			self.menuPanel.hidden = YES;
			if(self.menuPanel.superview)
			{
				[self.menuPanel removeFromSuperview ];
			}
			self.menuPanel = nil;
		}
		self.menuPanel = [self createMenuItem];
	}
	else if([response isEqualAction:kHO_ACTION_AddExternalFault])
	{
		[self cleanup];
		[self popupHintView:NSLocalizedString(@"申请成功!", @"申请成功!") inTime:2];
		[baseTableview reloadData];
	}
		
		
}

- (void)hintDidHide
{
//	[self goBack];
	[self.navigationController popToRootViewControllerAnimated:YES];
}


@end

